home *** CD-ROM | disk | FTP | other *** search
- unit SerNum;
-
- Interface
-
- uses WinProcs, WinTypes;
-
- function GetSerialNumber (drive: Byte): LongInt;
- function SetSerialNumber (drive: Byte; serNum: LongInt): Bool;
-
- Implementation
-
- type
- PMIDINFO = ^MIDINFO;
- MIDINFO = record
- InfoLevel: Word;
- SerialNum: Longint;
- VolLabel: array[0..10] of Char;
- FileSystem: array [0..7] of Char;
- end;
-
- var
- R: record { Real mode call structure }
- di, si, bp, Reserved, bx, dx, cx, ax : Longint;
- Flags, es, ds, fs, gs, ip, sp, ss: Word;
- end;
-
- {----------------------------------------------------------------------------}
- { Name: GetSetMid }
- { Purpose: Low level code to get or set a MIDINFO data structure for the }
- { specified drive. RealModeAX = $6900 for a get and $6901 for a }
- { set operation. }
- {----------------------------------------------------------------------------}
-
- function GetSetMid (Drive: Byte; MID: PMIDINFO; RealModeAX: Word): Bool;
- var
- Error: Byte;
- begin
- { Assume everything ok }
-
- Error := 0;
- GetSetMid := True;
-
- R.ax := RealModeAX;
- R.bx := Drive;
- R.ds := HiWord (Longint (MID)); { Subtle !!! }
- R.dx := LoWord (Longint (MID));
-
- asm
- mov bx, 0021h { set flags to $00, Real mode interrupt $21 }
- xor cx, cx { copy 0 words from protected mode stack }
- mov ax, seg R
- mov es, ax { selector of real mode call structure }
- mov di, offset R { offset of real mode call structure }
- mov ax, 0300h { DPMI simulate real mode interrupt }
- int 31h { do the business }
- jnc @@1 { branch if no error }
- inc Error
- @@1:
- end;
-
- if Error = 1 then GetSetMid := False;
- end;
-
- {----------------------------------------------------------------------------}
- { Name: GetMid }
- { Purpose: Get the MIDINFO record for a specified drive. }
- { Uses GetSetMid. Returns TRUE if successful. }
- {----------------------------------------------------------------------------}
-
- function GetMid (drive: Byte; var mid: MIDINFO): Bool;
- var
- p: LongInt;
- begin
- { Assume failure }
- GetMid := False;
-
- { Allocate a MIDINFO data structure in DOS address-space }
- p := GlobalDOSAlloc (sizeof (MIDINFO));
-
- if GetSetMid (drive, Ptr (HiWord (p), 0), $6900) then
- begin
- mid := PMIDINFO (Ptr (LoWord (p), 0))^;
- GetMid := True;
- end;
-
- GlobalDOSFree (LoWord (p));
- end;
-
- {----------------------------------------------------------------------------}
- { Name: SetMid }
- { Purpose: Set the MIDINFO record for a specified drive. }
- { Uses GetSetMid. Returns TRUE if successful. }
- {----------------------------------------------------------------------------}
-
- function SetMid (drive: Byte; var mid: MIDINFO): Bool;
- var
- p: LongInt;
- begin
- { Assume failure }
- SetMid := False;
-
- { Allocate a MIDINFO data structure in DOS address-space }
- p := GlobalDOSAlloc (sizeof (MIDINFO));
- PMIDINFO (Ptr (LoWord (p), 0))^ := mid;
- if GetSetMid (drive, Ptr (HiWord (p), 0), $6901) then SetMid := True;
- GlobalDOSFree (LoWord (p));
- end;
-
- {----------------------------------------------------------------------------}
- { Name: GetSerialNumber }
- { Purpose: Get the serial number for a specified drive. }
- { If an error occurs, then 0 is returned as the serial number. }
- {----------------------------------------------------------------------------}
-
- function GetSerialNumber (drive: Byte): LongInt;
- var
- mid: MIDINFO;
- begin
- if GetMid (drive, mid) then GetSerialNumber := mid.SerialNum
- else GetSerialNumber := 0;
- end;
-
- {----------------------------------------------------------------------------}
- { Name: SetSerialNumber }
- { Purpose: Set the serial number for a specified drive. }
- { If no error, TRUE is returned as the function result. }
- {----------------------------------------------------------------------------}
-
- function SetSerialNumber (drive: Byte; serNum: LongInt): Bool;
- var
- mid: MIDINFO;
- begin
- SetSerialNumber := False;
- if GetMid (drive, mid) then
- begin
- mid.SerialNum := serNum;
- SetSerialNumber := SetMid (drive, mid);
- end;
- end;
-
- end.
-